//package com.group.app.example.es.impl;
//
//import com.group.app.example.es.KeywordSearch;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.script.Script;
//import org.elasticsearch.search.SearchHits;
//import org.elasticsearch.search.aggregations.Aggregation;
//import org.elasticsearch.search.aggregations.AggregationBuilders;
//import org.elasticsearch.search.aggregations.Aggregations;
//import org.elasticsearch.search.aggregations.bucket.terms.Terms;
//import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
//import org.elasticsearch.search.aggregations.metrics.ParsedSum;
//import org.elasticsearch.search.aggregations.metrics.Sum;
//import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Repository;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
////@Repository
//public class KeywordSearchImpl implements KeywordSearch {
//
//    @Autowired
//    ElasticsearchHandlerImpl elasticsearchHandler;
//
//    @Autowired
//    private RestHighLevelClient restHighLevelClient;
//
//    private List<String> orderfield=new ArrayList(){
//        {add("channel");
//         add("business");}
//    };
//
//    @Override
//    public SearchResponse searchSummary(String beginDate, String endDate, String channel, String keyword, int size, int from) {
//        return null;
//    }
//
//    @Override
//    public SearchResponse searchOrderByKeyword(String beginDate, String endDate, List<String> channel, List<String> business, String keyword, int size, int from) {
//        String index="sss";
//        SearchSourceBuilder searchSourceBuilder=elasticsearchHandler.getSourceBuilder(size,from);
//        QueryBuilder queryBuilder=elasticsearchHandler.getQueryBuilderByKeyword(beginDate,endDate,channel,business,keyword);
//        HighlightBuilder highlightBuilder = elasticsearchHandler.getHighlightBuilder(orderfield);
//        return elasticsearchHandler.getSearchResponse(index,searchSourceBuilder,queryBuilder,highlightBuilder);
//    }
//
//    /*
//    * GET zkinfo/_search
//{
//  "query": {
//    "bool": {
//      "must": [
//        {
//          "term": {
//            "status": "30"
//          }
//        },
//        {
//          "bool": {
//            "should": [
//              {
//                "bool": {
//                  "must": [
//                    {
//                      "term": {
//                        "secondhandoverstatus": "0"
//                      }
//                    }
//                  ]
//                }
//              },
//              {
//                "bool": {
//                  "must": [
//                    {
//                      "exists": {
//                        "field": "secondhandoverstatus"
//                      }
//                    }
//                  ]
//                }
//              }
//            ]
//          }
//        }
//      ]
//    }
//  }
//}
//    * */
//    @Override
//    public Map<String, Object> getHandoverRequestStat() throws IOException {
//        BoolQueryBuilder superBoolQueryBuilder=new BoolQueryBuilder();
//        superBoolQueryBuilder.must(QueryBuilders.termQuery("status",180));
//        BoolQueryBuilder subBoolQueryBuilder=new BoolQueryBuilder();
//        BoolQueryBuilder subBoolQueryBuilder1=new BoolQueryBuilder()
//                .must(QueryBuilders.termQuery("secondhandoverstatus",0));
//        BoolQueryBuilder subBoolQueryBuilder2=new BoolQueryBuilder()
//                .mustNot(QueryBuilders.existsQuery("secondhandoverstatus"));
//        subBoolQueryBuilder.should(subBoolQueryBuilder1).should(subBoolQueryBuilder2);
//        superBoolQueryBuilder.must(subBoolQueryBuilder);
//        return parseRequest(superBoolQueryBuilder);
//    }
//
//    private Map<String,Object> parseRequest(BoolQueryBuilder boolQueryBuilder) throws IOException{
//        SearchRequest searchRequest=new SearchRequest("zkinfo");
//        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder().trackTotalHits(true).size(0);
//        SumAggregationBuilder sumAggregationBuilder= AggregationBuilders.sum("amount").field("charge");
//        searchSourceBuilder.query(boolQueryBuilder);
//        searchSourceBuilder.aggregation(sumAggregationBuilder);
//        searchRequest.source(searchSourceBuilder);
//        SearchResponse searchResponse=restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        SearchHits searchHits=searchResponse.getHits();
//        Sum sum=searchResponse.getAggregations().get("amount");
//        double amount=sum.getValue();
//        long total=searchHits.getTotalHits().value;
//        Map<String,Object> map=new HashMap<>();
//        map.put("total",total);
//        map.put("amount",(double)Math.round(amount*100)/100);
//        return map;
//    }
//
//
//    /**
//     * GET zkinfo/_search
//     * {
//     *   "query": {
//     *     "bool": {
//     *       "must": [
//     *         {
//     *           "term": {
//     *             "status": "30"
//     *           }
//     *         }
//     *       ]
//     *     }
//     *   },
//     *   "aggs": {
//     *     "group_by_message": {
//     *       "terms": {
//     *         "script":{
//     *           "source": "doc['servicefeature'].value+'_'+doc['area'].value+'_'doc['group'].value",
//     *           "lang": "painless"
//     *         },
//     *         "size": 10
//     *       },
//     *       "aggs": {
//     *         "amount": {
//     *           "sum": {
//     *             "field": "charge"
//     *           }
//     *         }
//     *       }
//     *     }
//     *   }
//     * }
//     */
//    private Map<String,Map<String,Object>> parseGroupRequest(BoolQueryBuilder boolQueryBuilder) throws IOException{
//        SearchRequest searchRequest=new SearchRequest("zkinfo");
//        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder().trackTotalHits(true).size(0);
//        TermsAggregationBuilder groupByMessage=AggregationBuilders
//                .terms("group_by_message")
//                .script(new Script("doc['servicefeature'].value+'_'+doc['area'].value+'_'doc['group'].value"))
//                .size(10);
//        SumAggregationBuilder sumAggregationBuilder= AggregationBuilders.sum("amount").field("charge");
//        groupByMessage.subAggregation(sumAggregationBuilder);
//        searchSourceBuilder.query(boolQueryBuilder);
//        searchSourceBuilder.aggregation(groupByMessage);
//        searchRequest.source(searchSourceBuilder);
//        SearchResponse searchResponse=restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        Aggregations aggregations=searchResponse.getAggregations();
//        Terms terms=aggregations.get("group_by_message");
//        List<? extends Terms.Bucket> buckets=terms.getBuckets();
//        Map<String,Map<String,Object>> result=new HashMap<>();
//        for(Terms.Bucket bucket:buckets){
//            String key=bucket.getKeyAsString();
//            long total=bucket.getDocCount();
//            Map<String, Aggregation> map = bucket.getAggregations().getAsMap();
//            Map<String,Object> tmp=new HashMap<>();
//            double amount=((ParsedSum)map.get("amount")).getValue();
//            tmp.put("total",total);
//            tmp.put("amount",(double)Math.round(amount*100)/100);
//            result.put(key,tmp);
//        }
//        return result;
//    }
//
//
//    private SearchSourceBuilder getSourceBuilder(int size,int from){
//        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
//        if (size>=0){
//            searchSourceBuilder.size(size);
//        }
//        if (from>=0){
//            searchSourceBuilder.from(from);
//        }
//        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//        return searchSourceBuilder;
//    }
//
//    private QueryBuilder getQueryBuilder(String beginDate,String endDate,String channel,String business,String keyword){
//        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
//        boolQueryBuilder
//                .filter(QueryBuilders
//                        .rangeQuery("rangeDate")
//                        .gte(beginDate)
//                        .lte(endDate)
//                        .timeZone("+08:00")
//                        .format("yyyyMMdd")
//                )
//                .filter(
//                        QueryBuilders.termQuery("channel",channel)
//                )
//                .filter(QueryBuilders.termQuery("business",business))
//                .must(QueryBuilders.multiMatchQuery(keyword));
//        return boolQueryBuilder;
//    }
//}
